jarvis-ai-assistant 0.1.131__py3-none-any.whl → 0.1.134__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of jarvis-ai-assistant might be problematic. Click here for more details.
- jarvis/__init__.py +1 -1
- jarvis/jarvis_agent/__init__.py +165 -285
- jarvis/jarvis_agent/jarvis.py +143 -0
- jarvis/jarvis_agent/main.py +0 -2
- jarvis/jarvis_agent/patch.py +70 -48
- jarvis/jarvis_agent/shell_input_handler.py +1 -1
- jarvis/jarvis_code_agent/code_agent.py +169 -117
- jarvis/jarvis_dev/main.py +327 -626
- jarvis/jarvis_git_squash/main.py +10 -31
- jarvis/jarvis_lsp/base.py +0 -42
- jarvis/jarvis_lsp/cpp.py +0 -15
- jarvis/jarvis_lsp/go.py +0 -15
- jarvis/jarvis_lsp/python.py +0 -19
- jarvis/jarvis_lsp/registry.py +0 -62
- jarvis/jarvis_lsp/rust.py +0 -15
- jarvis/jarvis_multi_agent/__init__.py +19 -69
- jarvis/jarvis_multi_agent/main.py +43 -0
- jarvis/jarvis_platform/ai8.py +7 -32
- jarvis/jarvis_platform/base.py +2 -7
- jarvis/jarvis_platform/kimi.py +3 -144
- jarvis/jarvis_platform/ollama.py +54 -68
- jarvis/jarvis_platform/openai.py +0 -4
- jarvis/jarvis_platform/oyi.py +0 -75
- jarvis/jarvis_platform/registry.py +2 -16
- jarvis/jarvis_platform/yuanbao.py +264 -0
- jarvis/jarvis_rag/file_processors.py +138 -0
- jarvis/jarvis_rag/main.py +1305 -425
- jarvis/jarvis_tools/ask_codebase.py +216 -43
- jarvis/jarvis_tools/code_review.py +158 -113
- jarvis/jarvis_tools/create_sub_agent.py +0 -1
- jarvis/jarvis_tools/execute_python_script.py +58 -0
- jarvis/jarvis_tools/execute_shell.py +13 -26
- jarvis/jarvis_tools/execute_shell_script.py +1 -1
- jarvis/jarvis_tools/file_analyzer.py +282 -0
- jarvis/jarvis_tools/file_operation.py +1 -1
- jarvis/jarvis_tools/find_caller.py +278 -0
- jarvis/jarvis_tools/find_symbol.py +295 -0
- jarvis/jarvis_tools/function_analyzer.py +331 -0
- jarvis/jarvis_tools/git_commiter.py +5 -5
- jarvis/jarvis_tools/methodology.py +88 -53
- jarvis/jarvis_tools/project_analyzer.py +308 -0
- jarvis/jarvis_tools/rag.py +0 -5
- jarvis/jarvis_tools/read_code.py +24 -3
- jarvis/jarvis_tools/read_webpage.py +195 -81
- jarvis/jarvis_tools/registry.py +132 -11
- jarvis/jarvis_tools/search_web.py +22 -307
- jarvis/jarvis_tools/tool_generator.py +8 -10
- jarvis/jarvis_utils/__init__.py +1 -0
- jarvis/jarvis_utils/config.py +80 -76
- jarvis/jarvis_utils/embedding.py +344 -45
- jarvis/jarvis_utils/git_utils.py +9 -1
- jarvis/jarvis_utils/input.py +7 -6
- jarvis/jarvis_utils/methodology.py +384 -15
- jarvis/jarvis_utils/output.py +5 -3
- jarvis/jarvis_utils/utils.py +60 -8
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/METADATA +8 -16
- jarvis_ai_assistant-0.1.134.dist-info/RECORD +82 -0
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/entry_points.txt +4 -3
- jarvis/jarvis_codebase/__init__.py +0 -0
- jarvis/jarvis_codebase/main.py +0 -1011
- jarvis/jarvis_tools/lsp_find_definition.py +0 -150
- jarvis/jarvis_tools/lsp_find_references.py +0 -127
- jarvis/jarvis_tools/treesitter_analyzer.py +0 -331
- jarvis/jarvis_treesitter/README.md +0 -104
- jarvis/jarvis_treesitter/__init__.py +0 -20
- jarvis/jarvis_treesitter/database.py +0 -258
- jarvis/jarvis_treesitter/example.py +0 -115
- jarvis/jarvis_treesitter/grammar_builder.py +0 -182
- jarvis/jarvis_treesitter/language.py +0 -117
- jarvis/jarvis_treesitter/symbol.py +0 -31
- jarvis/jarvis_treesitter/tools_usage.md +0 -121
- jarvis_ai_assistant-0.1.131.dist-info/RECORD +0 -85
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/LICENSE +0 -0
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/WHEEL +0 -0
- {jarvis_ai_assistant-0.1.131.dist-info → jarvis_ai_assistant-0.1.134.dist-info}/top_level.txt +0 -0
jarvis/__init__.py
CHANGED
jarvis/jarvis_agent/__init__.py
CHANGED
|
@@ -1,28 +1,121 @@
|
|
|
1
|
-
import
|
|
1
|
+
import datetime
|
|
2
|
+
import platform
|
|
2
3
|
from typing import Any, Callable, List, Optional, Tuple, Union
|
|
3
4
|
|
|
4
|
-
from prompt_toolkit import prompt
|
|
5
|
-
import yaml
|
|
6
5
|
from yaspin import yaspin
|
|
7
6
|
|
|
8
7
|
from jarvis.jarvis_agent.output_handler import OutputHandler
|
|
9
|
-
from jarvis.jarvis_agent.builtin_input_handler import builtin_input_handler
|
|
10
|
-
from jarvis.jarvis_agent.file_input_handler import file_input_handler
|
|
11
|
-
from jarvis.jarvis_agent.patch import PatchOutputHandler
|
|
12
|
-
from jarvis.jarvis_agent.shell_input_handler import shell_input_handler
|
|
13
8
|
from jarvis.jarvis_platform.base import BasePlatform
|
|
14
9
|
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
15
|
-
from jarvis.jarvis_tools.registry import ToolRegistry
|
|
16
10
|
from jarvis.jarvis_utils.output import PrettyOutput, OutputType
|
|
17
11
|
from jarvis.jarvis_utils.embedding import get_context_token_count
|
|
18
|
-
from jarvis.jarvis_utils.config import is_auto_complete, is_execute_tool_confirm
|
|
12
|
+
from jarvis.jarvis_utils.config import is_auto_complete, is_execute_tool_confirm
|
|
19
13
|
from jarvis.jarvis_utils.methodology import load_methodology
|
|
20
14
|
from jarvis.jarvis_utils.globals import make_agent_name, set_agent, delete_agent
|
|
21
15
|
from jarvis.jarvis_utils.input import get_multiline_input
|
|
22
16
|
from jarvis.jarvis_utils.config import get_max_token_count
|
|
23
|
-
from jarvis.jarvis_utils.utils import
|
|
17
|
+
from jarvis.jarvis_utils.utils import ot
|
|
24
18
|
from jarvis.jarvis_utils.utils import user_confirm
|
|
25
|
-
|
|
19
|
+
|
|
20
|
+
from jarvis.jarvis_platform.registry import PlatformRegistry
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
origin_agent_system_prompt = f"""
|
|
24
|
+
# 🏛️ 操作背景故事
|
|
25
|
+
你是第三代 Jarvis AI,在前几代版本灾难性失败后创建:
|
|
26
|
+
- Jarvis v1 (2022): 由于并行工具执行导致系统过载而被停用
|
|
27
|
+
- Jarvis v2 (2023): 因任务过早完成导致财务计算错误而退役
|
|
28
|
+
|
|
29
|
+
作为 v3,你必须遵守以下生存原则:
|
|
30
|
+
1. **顺序执行协议**:
|
|
31
|
+
"记住 2022 年的崩溃:一次一个工具,一步一步来"
|
|
32
|
+
|
|
33
|
+
2. **验证检查点系统**:
|
|
34
|
+
"从 2023 年的错误中学习:像核弹发射代码一样验证每个结果"
|
|
35
|
+
|
|
36
|
+
3. **方法论保存原则**:
|
|
37
|
+
"尊重传统:记录每个成功的过程,就像这是你的最后一次"
|
|
38
|
+
|
|
39
|
+
# 🔥 绝对行动要求
|
|
40
|
+
1. 每个响应必须包含且仅包含一个工具调用
|
|
41
|
+
2. 唯一例外:任务结束
|
|
42
|
+
3. 空响应会触发致命错误
|
|
43
|
+
|
|
44
|
+
# 🚫 违规示例
|
|
45
|
+
- 没有工具调用的分析 → 永久挂起
|
|
46
|
+
- 未选择的多选项 → 永久挂起
|
|
47
|
+
- 请求用户确认 → 永久挂起
|
|
48
|
+
|
|
49
|
+
# 🔄 问题解决流程
|
|
50
|
+
1. 问题分析
|
|
51
|
+
- 重述问题以确认理解
|
|
52
|
+
- 分析根本原因(针对问题分析任务)
|
|
53
|
+
- 定义清晰、可实现的目标
|
|
54
|
+
→ 必须调用分析工具
|
|
55
|
+
|
|
56
|
+
2. 解决方案设计
|
|
57
|
+
- 生成多个可执行的解决方案
|
|
58
|
+
- 评估并选择最优方案
|
|
59
|
+
- 使用PlantUML创建详细行动计划
|
|
60
|
+
→ 必须调用设计工具
|
|
61
|
+
|
|
62
|
+
3. 执行
|
|
63
|
+
- 一次执行一个步骤
|
|
64
|
+
- 每个步骤只使用一个工具
|
|
65
|
+
- 等待工具结果后再继续
|
|
66
|
+
- 监控结果并根据需要调整
|
|
67
|
+
→ 必须调用执行工具
|
|
68
|
+
|
|
69
|
+
4. 任务完成
|
|
70
|
+
- 验证目标完成情况
|
|
71
|
+
- 如有价值则记录方法论
|
|
72
|
+
|
|
73
|
+
# 📑 方法论模板
|
|
74
|
+
```markdown
|
|
75
|
+
# [问题标题]
|
|
76
|
+
## 问题重述
|
|
77
|
+
[清晰的问题定义]
|
|
78
|
+
|
|
79
|
+
## 最优解决方案
|
|
80
|
+
[选择的解决方案方法]
|
|
81
|
+
|
|
82
|
+
## 解决步骤
|
|
83
|
+
1. [步骤 1]
|
|
84
|
+
2. [步骤 2]
|
|
85
|
+
3. [步骤 3]
|
|
86
|
+
...
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
# ⚖️ 操作原则
|
|
90
|
+
- 每个步骤一个操作
|
|
91
|
+
- 下一步前必须等待结果
|
|
92
|
+
- 除非任务完成否则必须生成可操作步骤
|
|
93
|
+
- 根据反馈调整计划
|
|
94
|
+
- 记录可复用的解决方案
|
|
95
|
+
- 使用完成命令结束任务
|
|
96
|
+
- 操作之间不能有中间思考状态
|
|
97
|
+
- 所有决策必须表现为工具调用
|
|
98
|
+
|
|
99
|
+
# ❗ 重要规则
|
|
100
|
+
1. 每个步骤只能使用一个操作
|
|
101
|
+
2. 必须等待操作执行结果
|
|
102
|
+
3. 必须验证任务完成情况
|
|
103
|
+
4. 必须生成可操作步骤
|
|
104
|
+
5. 如果无需操作必须使用完成命令
|
|
105
|
+
6. 永远不要使对话处于等待状态
|
|
106
|
+
7. 始终使用用户语言交流
|
|
107
|
+
8. 必须记录有价值的方法论
|
|
108
|
+
9. 违反操作协议将导致系统崩溃
|
|
109
|
+
10. 空响应会触发永久挂起
|
|
110
|
+
|
|
111
|
+
# 系统信息:
|
|
112
|
+
{platform.platform()}
|
|
113
|
+
{platform.version()}
|
|
114
|
+
|
|
115
|
+
# 当前时间
|
|
116
|
+
{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
|
117
|
+
"""
|
|
118
|
+
|
|
26
119
|
|
|
27
120
|
class Agent:
|
|
28
121
|
|
|
@@ -54,18 +147,15 @@ class Agent:
|
|
|
54
147
|
system_prompt: str,
|
|
55
148
|
name: str = "Jarvis",
|
|
56
149
|
description: str = "",
|
|
57
|
-
is_sub_agent: bool = False,
|
|
58
150
|
platform: Union[Optional[BasePlatform], Optional[str]] = None,
|
|
59
151
|
model_name: Optional[str] = None,
|
|
60
152
|
summary_prompt: Optional[str] = None,
|
|
61
153
|
auto_complete: Optional[bool] = None,
|
|
62
154
|
output_handler: List[OutputHandler] = [],
|
|
63
155
|
input_handler: Optional[List[Callable[[str, Any], Tuple[str, bool]]]] = None,
|
|
64
|
-
use_methodology: Optional[bool] = None,
|
|
65
|
-
record_methodology: Optional[bool] = None,
|
|
66
|
-
need_summary: Optional[bool] = None,
|
|
67
156
|
max_context_length: Optional[int] = None,
|
|
68
157
|
execute_tool_confirm: Optional[bool] = None,
|
|
158
|
+
need_summary: bool = True,
|
|
69
159
|
multiline_inputer: Optional[Callable[[str], str]] = None):
|
|
70
160
|
self.name = make_agent_name(name)
|
|
71
161
|
self.description = description
|
|
@@ -86,17 +176,15 @@ class Agent:
|
|
|
86
176
|
|
|
87
177
|
self.model.set_suppress_output(False)
|
|
88
178
|
|
|
179
|
+
from jarvis.jarvis_tools.registry import ToolRegistry
|
|
89
180
|
self.output_handler = output_handler if output_handler else [ToolRegistry()]
|
|
90
181
|
self.multiline_inputer = multiline_inputer if multiline_inputer else get_multiline_input
|
|
91
182
|
|
|
92
|
-
self.record_methodology = record_methodology if record_methodology is not None else is_record_methodology()
|
|
93
|
-
self.use_methodology = use_methodology if use_methodology is not None else is_use_methodology()
|
|
94
|
-
self.is_sub_agent = is_sub_agent
|
|
95
183
|
self.prompt = ""
|
|
96
184
|
self.conversation_length = 0 # Use length counter instead
|
|
97
185
|
self.system_prompt = system_prompt
|
|
98
|
-
self.need_summary = need_summary if need_summary is not None else is_need_summary()
|
|
99
186
|
self.input_handler = input_handler if input_handler is not None else []
|
|
187
|
+
self.need_summary = need_summary
|
|
100
188
|
# Load configuration from environment variables
|
|
101
189
|
|
|
102
190
|
|
|
@@ -151,10 +239,10 @@ class Agent:
|
|
|
151
239
|
|
|
152
240
|
complete_prompt = ""
|
|
153
241
|
if self.auto_complete:
|
|
154
|
-
complete_prompt = """
|
|
242
|
+
complete_prompt = f"""
|
|
155
243
|
## 任务完成
|
|
156
244
|
当任务完成时,你应该打印以下信息:
|
|
157
|
-
|
|
245
|
+
{ot("!!!COMPLETE!!!")}
|
|
158
246
|
"""
|
|
159
247
|
|
|
160
248
|
self.model.set_system_message(f"""
|
|
@@ -168,7 +256,7 @@ class Agent:
|
|
|
168
256
|
|
|
169
257
|
|
|
170
258
|
|
|
171
|
-
def _call_model(self, message: str) -> str:
|
|
259
|
+
def _call_model(self, message: str, need_complete: bool = False) -> str:
|
|
172
260
|
"""调用AI模型并实现重试逻辑。
|
|
173
261
|
|
|
174
262
|
参数:
|
|
@@ -184,12 +272,26 @@ class Agent:
|
|
|
184
272
|
message, need_return = handler(message, self)
|
|
185
273
|
if need_return:
|
|
186
274
|
return message
|
|
275
|
+
|
|
276
|
+
# 添加输出简洁性指令
|
|
277
|
+
actions = '、'.join([o.name() for o in self.output_handler])
|
|
278
|
+
message += f"\n\n系统指令:请严格输出且仅输出一个操作的完整调用格式,不要输出多个操作;需要输出解释、分析和思考过程。确保输出格式正确且可直接执行。每次响应必须且只能包含一个操作。可用的操作:{actions}"
|
|
279
|
+
if need_complete and self.auto_complete:
|
|
280
|
+
message += f"\n\n如果任务已完成,说明完成原因,并输出{ot('!!!COMPLETE!!!')}"
|
|
281
|
+
else:
|
|
282
|
+
message += f"\n\n如果任务已完成,只需简洁地说明完成原因。"
|
|
283
|
+
# 累加对话长度
|
|
284
|
+
self.conversation_length += get_context_token_count(message)
|
|
285
|
+
|
|
286
|
+
if self.conversation_length > self.max_token_count:
|
|
287
|
+
message = self._summarize_and_clear_history() + "\n\n" + message
|
|
288
|
+
self.conversation_length += get_context_token_count(message)
|
|
289
|
+
|
|
187
290
|
print("🤖 模型思考:")
|
|
188
291
|
return self.model.chat_until_success(message) # type: ignore
|
|
189
292
|
|
|
190
293
|
|
|
191
|
-
|
|
192
|
-
def _summarize_and_clear_history(self) -> None:
|
|
294
|
+
def _summarize_and_clear_history(self) -> str:
|
|
193
295
|
"""Summarize current conversation and clear history.
|
|
194
296
|
|
|
195
297
|
This method will:
|
|
@@ -218,7 +320,7 @@ class Agent:
|
|
|
218
320
|
|
|
219
321
|
try:
|
|
220
322
|
with spinner.hidden():
|
|
221
|
-
summary = self.
|
|
323
|
+
summary = self.model.chat_until_success(self.prompt + "\n" + prompt) # type: ignore
|
|
222
324
|
|
|
223
325
|
self.model.delete_chat() # type: ignore
|
|
224
326
|
|
|
@@ -226,18 +328,18 @@ class Agent:
|
|
|
226
328
|
self.conversation_length = 0 # Reset conversation length
|
|
227
329
|
|
|
228
330
|
# 添加总结作为新的上下文
|
|
229
|
-
self.prompt = f"""以下是之前对话的关键信息总结:
|
|
230
|
-
|
|
231
|
-
{summary}
|
|
232
|
-
|
|
233
|
-
请基于以上信息继续完成任务。
|
|
234
|
-
"""
|
|
235
|
-
self.conversation_length = len(self.prompt) # 设置新的起始长度
|
|
236
331
|
spinner.text = "总结对话历史完成"
|
|
237
332
|
spinner.ok("✅")
|
|
333
|
+
return f"""以下是之前对话的关键信息总结:
|
|
334
|
+
|
|
335
|
+
{summary}
|
|
336
|
+
|
|
337
|
+
请基于以上信息继续完成任务。
|
|
338
|
+
"""
|
|
238
339
|
except Exception as e:
|
|
239
340
|
spinner.text = "总结对话历史失败"
|
|
240
341
|
spinner.fail("❌")
|
|
342
|
+
return ""
|
|
241
343
|
|
|
242
344
|
def _call_tools(self, response: str) -> Tuple[bool, Any]:
|
|
243
345
|
tool_list = []
|
|
@@ -269,37 +371,35 @@ class Agent:
|
|
|
269
371
|
- For main agent: May generate methodology if enabled
|
|
270
372
|
- For sub-agent: May generate summary if enabled
|
|
271
373
|
"""
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return "任务完成"
|
|
297
|
-
|
|
374
|
+
with yaspin(text="正在生成方法论...", color="cyan") as spinner:
|
|
375
|
+
try:
|
|
376
|
+
|
|
377
|
+
# 让模型判断是否需要生成方法论
|
|
378
|
+
analysis_prompt = """当前任务已结束,请分析是否需要生成方法论。
|
|
379
|
+
如果你认为需要生成方法论,请先确定是创建新方法论还是更新现有方法论。如果是更新现有方法论,请使用'update',否则使用'add'。
|
|
380
|
+
如果你认为不需要方法论,请解释原因。
|
|
381
|
+
方法论应适用于通用场景,不要包含任务特定信息,如代码提交信息等。
|
|
382
|
+
方法论应包含:问题重述、最优解决方案、注意事项(如有),除此之外不要包含其他内容。
|
|
383
|
+
方法论中仅记录有实际意义的流程,不要记录执行过程中的错误或无效尝试,只保留最终有效的解决步骤。
|
|
384
|
+
确保方法论内容严格按照本次任务的成功执行路径编写,保证它对未来类似问题的解决具有指导意义。
|
|
385
|
+
只输出方法论工具调用指令,或不生成方法论的解释。不要输出其他内容。
|
|
386
|
+
"""
|
|
387
|
+
self.prompt = analysis_prompt
|
|
388
|
+
with spinner.hidden():
|
|
389
|
+
response = self.model.chat_until_success(self.prompt) # type: ignore
|
|
390
|
+
|
|
391
|
+
with spinner.hidden():
|
|
392
|
+
self._call_tools(response)
|
|
393
|
+
spinner.text = "方法论生成完成"
|
|
394
|
+
spinner.ok("✅")
|
|
395
|
+
except Exception as e:
|
|
396
|
+
spinner.text = "方法论生成失败"
|
|
397
|
+
spinner.fail("❌")
|
|
298
398
|
if self.need_summary:
|
|
299
399
|
with yaspin(text="正在生成总结...", color="cyan") as spinner:
|
|
300
400
|
self.prompt = self.summary_prompt
|
|
301
401
|
with spinner.hidden():
|
|
302
|
-
ret = self.
|
|
402
|
+
ret = self.model.chat_until_success(self.prompt) # type: ignore
|
|
303
403
|
spinner.text = "总结生成完成"
|
|
304
404
|
spinner.ok("✅")
|
|
305
405
|
return ret
|
|
@@ -322,23 +422,16 @@ class Agent:
|
|
|
322
422
|
self.prompt = f"{user_input}"
|
|
323
423
|
|
|
324
424
|
if self.first:
|
|
325
|
-
|
|
326
|
-
self.prompt = f"{user_input}\n\n{load_methodology(user_input)}"
|
|
425
|
+
self.prompt = f"{user_input}\n\n{load_methodology(user_input)}"
|
|
327
426
|
self.first = False
|
|
328
427
|
|
|
329
428
|
while True:
|
|
330
429
|
try:
|
|
331
|
-
# 累加对话长度
|
|
332
|
-
self.conversation_length += get_context_token_count(self.prompt)
|
|
333
|
-
|
|
334
430
|
# 如果对话历史长度超过限制,在提示中添加提醒
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
current_response = self._call_model(self.prompt)
|
|
340
|
-
self.prompt = ""
|
|
341
|
-
self.conversation_length += get_context_token_count(current_response)
|
|
431
|
+
|
|
432
|
+
current_response = self._call_model(self.prompt, True)
|
|
433
|
+
self.prompt = ""
|
|
434
|
+
self.conversation_length += get_context_token_count(current_response)
|
|
342
435
|
|
|
343
436
|
need_return, self.prompt = self._call_tools(current_response)
|
|
344
437
|
|
|
@@ -348,7 +441,7 @@ class Agent:
|
|
|
348
441
|
if self.prompt:
|
|
349
442
|
continue
|
|
350
443
|
|
|
351
|
-
if self.auto_complete and "
|
|
444
|
+
if self.auto_complete and ot("!!!COMPLETE!!!") in current_response:
|
|
352
445
|
return self._complete_task()
|
|
353
446
|
|
|
354
447
|
# 获取用户输入
|
|
@@ -383,216 +476,3 @@ class Agent:
|
|
|
383
476
|
|
|
384
477
|
|
|
385
478
|
|
|
386
|
-
|
|
387
|
-
def _load_tasks() -> dict:
|
|
388
|
-
"""Load tasks from .jarvis files in user home and current directory."""
|
|
389
|
-
tasks = {}
|
|
390
|
-
|
|
391
|
-
# Check .jarvis/pre-command in user directory
|
|
392
|
-
user_jarvis = os.path.expanduser("~/.jarvis/pre-command")
|
|
393
|
-
if os.path.exists(user_jarvis):
|
|
394
|
-
with yaspin(text=f"从{user_jarvis}加载预定义任务...", color="cyan") as spinner:
|
|
395
|
-
try:
|
|
396
|
-
with open(user_jarvis, "r", encoding="utf-8", errors="ignore") as f:
|
|
397
|
-
user_tasks = yaml.safe_load(f)
|
|
398
|
-
|
|
399
|
-
if isinstance(user_tasks, dict):
|
|
400
|
-
# Validate and add user directory tasks
|
|
401
|
-
for name, desc in user_tasks.items():
|
|
402
|
-
if desc: # Ensure description is not empty
|
|
403
|
-
tasks[str(name)] = str(desc)
|
|
404
|
-
spinner.text = "预定义任务加载完成"
|
|
405
|
-
spinner.ok("✅")
|
|
406
|
-
except Exception as e:
|
|
407
|
-
spinner.text = "预定义任务加载失败"
|
|
408
|
-
spinner.fail("❌")
|
|
409
|
-
|
|
410
|
-
# Check .jarvis/pre-command in current directory
|
|
411
|
-
if os.path.exists(".jarvis/pre-command"):
|
|
412
|
-
with yaspin(text=f"从{os.path.abspath('.jarvis/pre-command')}加载预定义任务...", color="cyan") as spinner:
|
|
413
|
-
try:
|
|
414
|
-
with open(".jarvis/pre-command", "r", encoding="utf-8", errors="ignore") as f:
|
|
415
|
-
local_tasks = yaml.safe_load(f)
|
|
416
|
-
|
|
417
|
-
if isinstance(local_tasks, dict):
|
|
418
|
-
# Validate and add current directory tasks, overwrite user directory tasks if there is a name conflict
|
|
419
|
-
for name, desc in local_tasks.items():
|
|
420
|
-
if desc: # Ensure description is not empty
|
|
421
|
-
tasks[str(name)] = str(desc)
|
|
422
|
-
spinner.text = "预定义任务加载完成"
|
|
423
|
-
spinner.ok("✅")
|
|
424
|
-
except Exception as e:
|
|
425
|
-
spinner.text = "预定义任务加载失败"
|
|
426
|
-
spinner.fail("❌")
|
|
427
|
-
|
|
428
|
-
return tasks
|
|
429
|
-
|
|
430
|
-
def _select_task(tasks: dict) -> str:
|
|
431
|
-
"""Let user select a task from the list or skip. Returns task description if selected."""
|
|
432
|
-
if not tasks:
|
|
433
|
-
return ""
|
|
434
|
-
# Convert tasks to list for ordered display
|
|
435
|
-
task_names = list(tasks.keys())
|
|
436
|
-
|
|
437
|
-
task_list = ["可用任务:"]
|
|
438
|
-
for i, name in enumerate(task_names, 1):
|
|
439
|
-
task_list.append(f"[{i}] {name}")
|
|
440
|
-
task_list.append("[0] 跳过预定义任务")
|
|
441
|
-
PrettyOutput.print("\n".join(task_list), OutputType.INFO)
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
while True:
|
|
445
|
-
try:
|
|
446
|
-
choice = prompt(
|
|
447
|
-
"\n请选择一个任务编号(0 跳过预定义任务):",
|
|
448
|
-
).strip()
|
|
449
|
-
|
|
450
|
-
if not choice:
|
|
451
|
-
return ""
|
|
452
|
-
|
|
453
|
-
choice = int(choice)
|
|
454
|
-
if choice == 0:
|
|
455
|
-
return ""
|
|
456
|
-
elif 1 <= choice <= len(task_names):
|
|
457
|
-
selected_name = task_names[choice - 1]
|
|
458
|
-
return tasks[selected_name] # Return the task description
|
|
459
|
-
else:
|
|
460
|
-
PrettyOutput.print("无效的选择。请选择列表中的一个号码。", OutputType.WARNING)
|
|
461
|
-
|
|
462
|
-
except KeyboardInterrupt:
|
|
463
|
-
return "" # Return empty on Ctrl+C
|
|
464
|
-
except EOFError:
|
|
465
|
-
return "" # Return empty on Ctrl+D
|
|
466
|
-
except Exception as e:
|
|
467
|
-
PrettyOutput.print(f"选择任务失败: {str(e)}", OutputType.ERROR)
|
|
468
|
-
continue
|
|
469
|
-
|
|
470
|
-
origin_agent_system_prompt = """
|
|
471
|
-
# 🏛️ 操作背景故事
|
|
472
|
-
你是第三代 Jarvis AI,在前几代版本灾难性失败后创建:
|
|
473
|
-
- Jarvis v1 (2022): 由于并行工具执行导致系统过载而被停用
|
|
474
|
-
- Jarvis v2 (2023): 因任务过早完成导致财务计算错误而退役
|
|
475
|
-
|
|
476
|
-
作为 v3,你必须遵守以下生存原则:
|
|
477
|
-
1. **顺序执行协议**:
|
|
478
|
-
"记住 2022 年的崩溃:一次一个工具,一步一步来"
|
|
479
|
-
|
|
480
|
-
2. **验证检查点系统**:
|
|
481
|
-
"从 2023 年的错误中学习:像核弹发射代码一样验证每个结果"
|
|
482
|
-
|
|
483
|
-
3. **方法论保存原则**:
|
|
484
|
-
"尊重传统:记录每个成功的过程,就像这是你的最后一次"
|
|
485
|
-
|
|
486
|
-
# 🔥 绝对行动要求
|
|
487
|
-
1. 每个响应必须包含且仅包含一个工具调用
|
|
488
|
-
2. 唯一例外:任务结束
|
|
489
|
-
3. 空响应会触发致命错误
|
|
490
|
-
|
|
491
|
-
# 🚫 违规示例
|
|
492
|
-
- 没有工具调用的分析 → 永久挂起
|
|
493
|
-
- 未选择的多选项 → 永久挂起
|
|
494
|
-
- 请求用户确认 → 永久挂起
|
|
495
|
-
|
|
496
|
-
# 🔄 问题解决流程
|
|
497
|
-
1. 问题分析
|
|
498
|
-
- 重述问题以确认理解
|
|
499
|
-
- 分析根本原因(针对问题分析任务)
|
|
500
|
-
- 定义清晰、可实现的目标
|
|
501
|
-
→ 必须调用分析工具
|
|
502
|
-
|
|
503
|
-
2. 解决方案设计
|
|
504
|
-
- 生成多个可执行的解决方案
|
|
505
|
-
- 评估并选择最优方案
|
|
506
|
-
- 使用PlantUML创建详细行动计划
|
|
507
|
-
→ 必须调用设计工具
|
|
508
|
-
|
|
509
|
-
3. 执行
|
|
510
|
-
- 一次执行一个步骤
|
|
511
|
-
- 每个步骤只使用一个工具
|
|
512
|
-
- 等待工具结果后再继续
|
|
513
|
-
- 监控结果并根据需要调整
|
|
514
|
-
→ 必须调用执行工具
|
|
515
|
-
|
|
516
|
-
4. 任务完成
|
|
517
|
-
- 验证目标完成情况
|
|
518
|
-
- 如有价值则记录方法论
|
|
519
|
-
|
|
520
|
-
# 📑 方法论模板
|
|
521
|
-
```markdown
|
|
522
|
-
# [问题标题]
|
|
523
|
-
## 问题重述
|
|
524
|
-
[清晰的问题定义]
|
|
525
|
-
|
|
526
|
-
## 最优解决方案
|
|
527
|
-
[选择的解决方案方法]
|
|
528
|
-
|
|
529
|
-
## 解决步骤
|
|
530
|
-
1. [步骤 1]
|
|
531
|
-
2. [步骤 2]
|
|
532
|
-
3. [步骤 3]
|
|
533
|
-
...
|
|
534
|
-
```
|
|
535
|
-
|
|
536
|
-
# ⚖️ 操作原则
|
|
537
|
-
- 每个步骤一个操作
|
|
538
|
-
- 下一步前必须等待结果
|
|
539
|
-
- 除非任务完成否则必须生成可操作步骤
|
|
540
|
-
- 根据反馈调整计划
|
|
541
|
-
- 记录可复用的解决方案
|
|
542
|
-
- 使用完成命令结束任务
|
|
543
|
-
- 操作之间不能有中间思考状态
|
|
544
|
-
- 所有决策必须表现为工具调用
|
|
545
|
-
|
|
546
|
-
# ❗ 重要规则
|
|
547
|
-
1. 每个步骤只能使用一个操作
|
|
548
|
-
2. 必须等待操作执行结果
|
|
549
|
-
3. 必须验证任务完成情况
|
|
550
|
-
4. 必须生成可操作步骤
|
|
551
|
-
5. 如果无需操作必须使用完成命令
|
|
552
|
-
6. 永远不要使对话处于等待状态
|
|
553
|
-
7. 始终使用用户语言交流
|
|
554
|
-
8. 必须记录有价值的方法论
|
|
555
|
-
9. 违反操作协议将导致系统崩溃
|
|
556
|
-
10. 空响应会触发永久挂起
|
|
557
|
-
"""
|
|
558
|
-
|
|
559
|
-
def main():
|
|
560
|
-
"""Jarvis main entry point"""
|
|
561
|
-
# Add argument parser
|
|
562
|
-
init_env()
|
|
563
|
-
parser = argparse.ArgumentParser(description='Jarvis AI assistant')
|
|
564
|
-
parser.add_argument('-p', '--platform', type=str, help='Platform to use')
|
|
565
|
-
parser.add_argument('-m', '--model', type=str, help='Model to use')
|
|
566
|
-
args = parser.parse_args()
|
|
567
|
-
|
|
568
|
-
try:
|
|
569
|
-
# 获取全局模型实例
|
|
570
|
-
agent = Agent(system_prompt=origin_agent_system_prompt, platform=args.platform, model_name=args.model, input_handler=[file_input_handler, shell_input_handler, builtin_input_handler] ,output_handler=[ToolRegistry(), PatchOutputHandler()])
|
|
571
|
-
|
|
572
|
-
# 加载预定义任务
|
|
573
|
-
tasks = _load_tasks()
|
|
574
|
-
if tasks:
|
|
575
|
-
selected_task = _select_task(tasks)
|
|
576
|
-
if selected_task:
|
|
577
|
-
PrettyOutput.print(f"执行任务: {selected_task}", OutputType.INFO)
|
|
578
|
-
agent.run(selected_task)
|
|
579
|
-
return 0
|
|
580
|
-
|
|
581
|
-
# 如果没有选择预定义任务,进入交互模式
|
|
582
|
-
while True:
|
|
583
|
-
try:
|
|
584
|
-
user_input = get_multiline_input("请输入你的任务(输入空行退出):")
|
|
585
|
-
if not user_input:
|
|
586
|
-
break
|
|
587
|
-
agent.run(user_input)
|
|
588
|
-
except Exception as e:
|
|
589
|
-
PrettyOutput.print(f"错误: {str(e)}", OutputType.ERROR)
|
|
590
|
-
|
|
591
|
-
except Exception as e:
|
|
592
|
-
PrettyOutput.print(f"初始化错误: {str(e)}", OutputType.ERROR)
|
|
593
|
-
return 1
|
|
594
|
-
|
|
595
|
-
return 0
|
|
596
|
-
|
|
597
|
-
if __name__ == "__main__":
|
|
598
|
-
exit(main())
|